import { io } from 'socket.io-client'
import {
  handleGetIceServersRequest,
  handleCallRequest,
  handleCreateOfferRequest,
  handleSetAnswerRequest,
  handleHangupRequest,
  handleAddIceCandidateRequest,
  handleGetIceCandidateRequest,
} from '../controllers/socket-controller.js'
import config from '../config/config.js'

// Create a socket
const socket = io(config.wsBaseURL)

// Connect
socket.on('connect', () => console.log('Server connected'))

// Disconnect
socket.on('disconnect', () => console.log('Server disconnected'))

// Get the ICE server configuration
socket.on('getIceServersRequest', (url) => handleGetIceServersRequest(socket, url))

// Send offer and get answer
socket.on('callRequest', (url, offer) => handleCallRequest(socket, url, offer))

// Create an offer
socket.on('createOfferRequest', (url) => handleCreateOfferRequest(socket, url))

// Set an answer
socket.on('setAnswerRequest', (url, answer) => handleSetAnswerRequest(socket, url, answer))

//  Close the call
socket.on('hangupRequest', (url) => handleHangupRequest(socket, url))

// Add ICE candidates
socket.on('addIceCandidateRequest', (url, candidate) => handleAddIceCandidateRequest(socket, url, candidate))

// Get a list of ICE candidates
socket.on('getIceCandidateRequest', (url) => handleGetIceCandidateRequest(socket, url))
